#pragma once
#include <souistd.h>
#include <interface/SFactory-i.h>
#include "qjsFuncSlot.h"

void Slog(const char* szLog) {
	SLOGI2("qjs") << szLog;
}


BOOL InitFileResProvider(IResProvider* pResProvider, const char* path)
{
	SStringT strPath = S_CA2T(path);
	return pResProvider->Init((WPARAM)strPath.c_str(), 0);
}

BOOL InitPEResProvider(IResProvider* pResProvider, const char* path)
{
	SStringT strPath = S_CA2T(path);
	return pResProvider->Init((WPARAM)strPath.c_str(), 1);
}

IApplication* GetApp() {
	return SApplication::getSingletonPtr();
}

void toWChar(const char* src, SStringW* dst) {
	SStringW wstr = S_CA2W(src);
	dst->Assign2(wstr.c_str(), wstr.GetLength());
}

HWND GetActiveWnd() {
	return GetActiveWindow();
}

BOOL SConnect(IWindow* pObj, int evtId, Value& jsThis, Value& jsFun) {
	IEvtSlot* pSlot = new QjsFuncSlot(jsThis, jsFun);
	BOOL bRet = pObj->SubscribeEvent(evtId, pSlot);
	pSlot->Release();
	return bRet;
}

int SMessageBoxA(HWND hOwner, LPCSTR pszText, LPCSTR pszTitle, UINT uType) {
	SStringT strText = S_CA2T(pszText);
	SStringT strTitle = S_CA2T(pszTitle);
	return SMessageBox(hOwner, strText, strTitle, uType);
}

void Exp_Global(qjsbind::Module* module)
{
	module->ExportFunc("log", &Slog);
	module->ExportFunc("GetApp", &GetApp);
	module->ExportFunc("GetActiveWindow", &GetActiveWnd);
	module->ExportFunc("toWChar", &toWChar);
	module->ExportFunc("InitFileResProvider", &InitFileResProvider);
	module->ExportFunc("InitPEResProvider", &InitPEResProvider);
	module->ExportFunc("CreateSouiFactory", &CreateSouiFactory);
	module->ExportFunc("SConnect", &SConnect);
	module->ExportFunc("SMessageBox", &SMessageBoxA);
}
